/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.order.web;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.javaeye.order.service.ICustomerService;
import com.javaeye.common.web.PageInfo;
import com.javaeye.order.dto.Customer;
import com.javaeye.store.dto.MaterialsCategory;
import com.javaeye.store.service.IMaterialsCategoryService;

public class CustomerAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1578291709804889611L;

	private static Log log = LogFactory.getLog(CustomerAction.class); 

	/**
	 * 用于显示一览结果集
	 */
	private List<Customer> customerList;
	
	/**
	 * 用于接收分页值对象
	 */
	private PageInfo pageInfo;
	
	/**
	 * 用于接收查询条件值对象
	 */
	private Customer condition;
	
	/**
	 * 用于接收添加画面和编辑画面的值对象
	 */
	private Customer customer;
	
	/**
	 * 用于接收需要修改和删除的ID值
	 */
	private String customerId;
	
	/**
	 * 用于生成查询画面上选择分类的下拉框的值
	 */
	private List<MaterialsCategory> categoryList;
	
	private Integer[] productCategorys;
	
	private int parentCategoryId;
	
	private List<MaterialsCategory> secondCategoryList;
	
	/**
	 * 应用autocomplete的查询使用接口
	 */
	private String q;
	
	/**
	 * Service
	 */
	private ICustomerService customerService;
	
	private IMaterialsCategoryService categoryService;
	
	public CustomerAction() {
		pageInfo = new PageInfo();
		pageInfo.setPageIndex(0);
		condition = new Customer();
		condition.setName("");
		condition.setArea("");
		condition.setConditionCategory("-1");
	}
	
	private String getProductCategoryNames(Set<MaterialsCategory> categorys) {
		StringBuffer categoryNames = new StringBuffer();
		for (MaterialsCategory category : categorys) {
			categoryNames.append(category.getName());
			categoryNames.append(" ");
		}
		return categoryNames.toString();
	}
	
	public String queryAutoCompleteNames() {
		customerList = customerService.getAutoCompleteCustomers(q, 10);
		return SUCCESS;
	}
	
	/**
	 * 条件分页查询
	 * @return
	 * @throws Exception
	 */
	public String queryCustomerList() throws Exception {
		log.debug("currentPage=" + pageInfo.getPageIndex() + 
				", area=" + condition.getArea() + 
				", name=" + condition.getName() +
				", Category=" + condition.getConditionCategory());
		categoryList = categoryService.getCategorys(0);
		customerList = customerService.getCustomerList(condition, pageInfo);
		Customer customer;
		for (Object o : customerList) {
			customer = (Customer)o;
			Set<MaterialsCategory> categorys = customer.getProductCategorys();
			if (categorys != null) {
				customer.setConditionCategory(getProductCategoryNames(categorys));
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 保存
	 * @return
	 * @throws Exception
	 */
	public String saveCustomer() throws Exception {
		if (customer.getArea() == null
				|| "".equals(customer.getArea().trim())) {
			addFieldError("customer.area", getText("fileErrorAreaIsNull"));
			categoryList = categoryService.getCategorys(0);
			return INPUT;
		}
		if (productCategorys != null) {
			Set<MaterialsCategory> categorys = new HashSet<MaterialsCategory>();
			for (int i : productCategorys) {
				MaterialsCategory pc = new MaterialsCategory();
				pc.setId(i);
				categorys.add(pc);
			}
			customer.setProductCategorys(categorys);
		}
		customer = customerService.saveCustomer(customer);
		return SUCCESS;
	}
	
	/**
	 * 查询用于编辑
	 * @return
	 * @throws Exception
	 */
	public String queryForEditCustomer() throws Exception {
		categoryList = categoryService.getCategorys(0);
		customer = customerService.getCustomer(customerId);
		
		Set<MaterialsCategory> categorys = customer.getProductCategorys();
		if (categorys != null) {
			ArrayList<Integer> ids = new ArrayList<Integer>();
			for (MaterialsCategory pc : categorys) {
				ids.add(pc.getId());
				parentCategoryId = pc.getParentId();
			}
			productCategorys = ids.toArray(new Integer[ids.size()]);
		}
		
		secondCategoryList = categoryService.getCategorys(parentCategoryId);
		
		return SUCCESS;
	}
	
	/**
	 * 查询用于添加
	 * @return
	 * @throws Exception
	 */
	public String queryForAddCustomer() throws Exception {
		categoryList = categoryService.getCategorys(0);
		secondCategoryList = new ArrayList<MaterialsCategory>();
		return SUCCESS;
	}
	
	/**
	 * 查询用于查看
	 * @return
	 * @throws Exception
	 */
	public String queryForViewCustomer() throws Exception {
		customer = customerService.getCustomer(customerId);
		Set<MaterialsCategory> categorys = customer.getProductCategorys();
		if (categorys != null) {
			customer.setConditionCategory(getProductCategoryNames(categorys));
		}
		return SUCCESS;
	}
	
	/**
	 * 删除
	 * @return
	 * @throws Exception
	 */
	public String deleteCustomer() throws Exception {
		boolean result = customerService.removeCustomer(customerId);
		queryCustomerList();
		if (!result) {
			List<Object> params = new ArrayList<Object>();
			params.add("客户");
			this.addActionError(getText("actionErrorDeleteFK", params));
			return INPUT;
		}
		return SUCCESS;
	}

	public List<Customer> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<Customer> customerList) {
		this.customerList = customerList;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	public Customer getCondition() {
		return condition;
	}

	public void setCondition(Customer condition) {
		this.condition = condition;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public List<MaterialsCategory> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<MaterialsCategory> categoryList) {
		this.categoryList = categoryList;
	}

	public Integer[] getProductCategorys() {
		return productCategorys;
	}

	public void setProductCategorys(Integer[] productCategorys) {
		this.productCategorys = productCategorys;
	}

	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	public void setCategoryService(IMaterialsCategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public void setParentCategoryId(int parentCategoryId) {
		this.parentCategoryId = parentCategoryId;
	}

	public int getParentCategoryId() {
		return parentCategoryId;
	}

	public void setSecondCategoryList(List<MaterialsCategory> secondCategoryList) {
		this.secondCategoryList = secondCategoryList;
	}

	public List<MaterialsCategory> getSecondCategoryList() {
		return secondCategoryList;
	}

	public String getQ() {
		return q;
	}

	public void setQ(String q) {
		this.q = q;
	}
	
}
